<template>
  <div class="app-container">
    <el-form ref="dishForm" :model="dishForm" :rules="dishRules" label-width="100px">
      <el-form-item label="菜品名称:" prop="name">
        <el-input v-model="dishForm.name" placeholder="请输入菜品名称" style="width:30%" />
      </el-form-item>
      <el-form-item label="菜品价格:" prop="price">
        <el-input v-model="dishForm.price" placeholder="请输入菜品价格" style="width:30%" />
      </el-form-item>
      <el-form-item label="菜品简介:">
        <el-input
          v-model="dishForm.resume"
          type="textarea"
          style="width:50%"
          placeholder="请输入菜品简介"
          :autosize="{ minRows: 2, maxRows: 4}"
        />
      </el-form-item>
      <el-form-item label="图片地址:" prop="photoUrl">
        <el-input v-model="dishForm.photoUrl" placeholder="请输入菜品地址" style="width:50%" />
      </el-form-item>
      <el-form-item label="热销:" prop="state">
        <el-switch v-model="dishForm.state" active-value="1" inactive-value="0" />
      </el-form-item>
      <el-form-item label="销售量:" prop="saleNum">
        <el-input-number v-model="dishForm.saleNum" controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="库存量:" prop="storeNum">
        <el-input-number v-model="dishForm.storeNum" controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="分类:" prop="dishesSeriesId">
        <el-select v-model="dishForm.dishesSeriesId" placeholder="请选择">
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button v-if="!isEdit" @click="onReset('dishForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getDishById, addDishes, updateDishes } from '@/api/dish'
import { getCategory } from '@/api/category'

const defaultDishForm = {
  name: '',
  price: '',
  resume: '',
  photoUrl: '',
  state: 0,
  saleNum: 0,
  storeNum: 0,
  dishesSeriesId: ''
}
export default {
  name: 'DishDetail',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const isDecimal = (rule, value, callback) => {
      if (!value) {
        callback(new Error('输入不能为空'))
      } else {
        if (!Number(value)) {
          callback(new Error('请输入数字'))
        } else {
          callback()
        }
      }
    }
    return {
      dishForm: Object.assign({}, defaultDishForm),
      categoryList: null,
      dishRules: {
        name: [{
          required: true, message: '请输入菜品名称', trigger: 'blur'
        }],
        price: [{
          required: true, trigger: 'blur', validator: isDecimal
        }],
        photoUrl: [{
          required: true, message: '请输入图片地址', trigger: 'blur'
        }],
        dishesSeriesId: [{
          required: true, message: '请输入菜品类别', trigger: 'blur'
        }]
      }
    }
  },
  created() {
    this.fetchData()
    if (this.isEdit) {
      getDishById(this.$route.query.id).then(res => {
        this.dishForm = res.data
      })
    }
  },
  methods: {
    fetchData() {
      getCategory().then(res => {
        this.categoryList = res.data
      })
    },
    onSubmit() {
      this.$refs.dishForm.validate(valid => {
        if (valid) {
          this.$confirm('是否要进行提交操作?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if (this.isEdit) {
              updateDishes(this.dishForm).then(res => {
                this.$message.success('更新成功')
              })
            } else {
              addDishes(this.dishForm).then(res => {
                this.$message.success('成功提交')
              })
            }
          })
        } else {
          this.$message.error('未填写完全')
          return false
        }
      })
    },
    onReset(formName) {
      this.$refs[formName].resetFields()
      this.dishForm = Object.assign({}, defaultDishForm)
      this.$message('重置')
    }
  }
}
</script>
<style lang="scss">
</style>
